package com.csu.ui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.csu.dao.MemberDao;
import com.csu.dao.MemberDaoImpl;

public class LoginUI {
	
	//主界面
	public static MainUI mainUI = new MainUI();
	
	public static void main(String[] args) {
		
		//登录窗口
		JFrame jFrame = new JFrame("家庭财务管理系统 -- 登录");
		jFrame.setSize(600, 400);
		jFrame.setResizable(false);
		jFrame.setLocationRelativeTo(null);
		
		//用户名面板
		ImageIcon acctImg = new ImageIcon("photo/AcctPic.png");
		JLabel acctLabel = new JLabel();
		acctLabel.setIcon(acctImg);
		acctLabel.setBounds(10, 10, 50, 50);
		JTextField acctField = new JTextField();
		acctField.setBounds(60, 20, 100, 30);
		JPanel panel1 = new JPanel();
		panel1.add(acctLabel);
		panel1.add(acctField);
		panel1.setLayout(null);
				
		//密码面板
		ImageIcon pwdImg = new ImageIcon("photo/PwdPic.png");
		JLabel pwdLabel = new JLabel();
		pwdLabel.setIcon(pwdImg);
		pwdLabel.setBounds(10, 10, 50, 50);
		JPasswordField pwdField = new JPasswordField();
		pwdField.setBounds(60, 20, 100, 30);
		JPanel panel2 = new JPanel();
		panel2.add(pwdLabel);
		panel2.add(pwdField);
		panel2.setLayout(null);
		
		//类型面板
		ImageIcon typeImg = new ImageIcon("photo/TypePic.png");
		JLabel typeLabel = new JLabel();
		typeLabel.setIcon(typeImg);
		typeLabel.setBounds(10, 10, 50, 50);
		JComboBox<String> typeBox = new JComboBox<String>();
		typeBox.addItem("管理员");
		typeBox.addItem("普通用户");
		typeBox.setSelectedItem("普通用户");
		typeBox.setBounds(60, 20, 100, 30);
		JPanel panel3 = new JPanel();
		panel3.add(typeLabel);
		panel3.add(typeBox);
		panel3.setLayout(null);
		
		//按钮
		JButton loginBtn = new JButton("登录");
		JButton regBtn = new JButton("注册");
		JPanel panel4 = new JPanel(new FlowLayout(FlowLayout.CENTER));
		panel4.add(loginBtn);
		panel4.add(regBtn);
		
		//图片
		ImageIcon loginImg = new ImageIcon("photo/LoginPic.png");
		JLabel imgLabel = new JLabel();
		imgLabel.setIcon(loginImg);
		
		//登录按钮事件
		loginBtn.addActionListener(new ActionListener() {		
			@Override
			public void actionPerformed(ActionEvent e) {
				MemberDao memberDao = new MemberDaoImpl();
				if(acctField.getText().equals("") || String.valueOf(pwdField.getPassword()).equals("")) {
				}
				else {
					if(memberDao.loginSelect(typeBox.getSelectedIndex(),acctField.getText(),String.valueOf(pwdField.getPassword()))==1) {
						JOptionPane.showMessageDialog(null,
								"登录成功！",
								"提示",JOptionPane.INFORMATION_MESSAGE);
						mainUI.setUserType(typeBox.getSelectedIndex());
						mainUI.setCurrAcct(acctField.getText());
						mainUI.homePage();
						mainUI.setVisible(true);
						jFrame.setVisible(false);
					}
					else {
						JOptionPane.showMessageDialog(null,
								"登录失败，可能由于以下原因导致："+'\n'+'\n'+
								"·用户名不存在"+'\n'+
								"·密码错误"+'\n'+
								"·用户类型错误",
								"提示",JOptionPane.ERROR_MESSAGE);
						pwdField.setText("");
						typeBox.setSelectedItem("普通用户");
						acctField.requestFocus();
						acctField.selectAll();
					}
				}	
		   	}
		});
		
		//注册按钮事件
		regBtn.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {
				RegUI regUI = new RegUI();
				regUI.setVisible(true);
			}
		});
		
		//创建容器
		Box vBox = Box.createVerticalBox();
		vBox.add(panel1);
		vBox.add(panel2);
		vBox.add(panel3);
		vBox.add(panel4);
		
		//布局管理
		jFrame.setLayout(null);
		jFrame.add(imgLabel);
		jFrame.add(vBox);
		imgLabel.setBounds(10,30,400,300);
		vBox.setBounds(420,60,170,280);
		
		//显示窗口
		mainUI.setVisible(false);
		jFrame.setVisible(true);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
}
